import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Req,
  Res,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { PublicResourceService } from './public_resource.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { CreatePublicResourceDto } from './dto/create-public_resource.dto';
import { Request } from 'express';
import { request, type Response } from 'express';
import { Public } from 'src/common/decorator/public.decorator';
import * as path from 'path';
import * as fs from 'fs';
import { UpdatePublicResourceDto } from './dto/update-public_resource.dto';

@Controller('public-resource')
export class PublicResourceController {
  constructor(private readonly publicResourceService: PublicResourceService) {}

  @Post('createPublicResourceItem')
  @UseInterceptors(FileInterceptor('file'))
  createPublicResourceItem(
    @Body() createPublicResourceDto: CreatePublicResourceDto,
    @UploadedFile() file,
    @Req() request: Request,
  ) {
    const user = request.user;
    createPublicResourceDto.uploader = (user as any).email;
    return this.publicResourceService.createPublicResourceItem(
      createPublicResourceDto,
      file,
    );
  }

  @Get('getAllPublicResource')
  getAllPublicResource() {
    return this.publicResourceService.getAllPublicResource();
  }

  @Get('getPublicResourceById/:id')
  getPublicResourceById(@Param('id') id: string) {
    return this.publicResourceService.getPublicResourceById(id);
  }

  @Patch('updatePublicResourceById/:id')
  updatePublicResourceById(
    @Param('id') id: string,
    @Body() updatePublicResourceDto: UpdatePublicResourceDto,
  ) {
    return this.publicResourceService.updatePublicResourceById(
      id,
      updatePublicResourceDto,
    );
  }

  @Delete('deletePublicResourceById/:id')
  deletePublicResourceById(@Param('id') id: string) {
    return this.publicResourceService.deletePublicResourceById(id);
  }

  @Public()
  @Get('getFileItemContent/:id')
  getFileItemContent(@Param('id') id: string, @Res() response: Response) {
    return this.publicResourceService.getFileItemContent(id, response);
  }

  @Public()
  @Get('downloadPublicResourceItemStream/:id')
  downloadPublicResourceItemStream(
    @Param('id') id: string,
    @Res() response: Response,
  ) {
    return this.publicResourceService.downloadPublicResourceItemStream(
      id,
      response,
    );
  }

  @Get('getPublicResourcPicture')
  getPublicResourcPicture() {
    return this.publicResourceService.getPublicResourcPicture();
  }

  @Get('getPublicResourcVideo')
  getPublicResourcVideo() {
    return this.publicResourceService.getPublicResourcVideo();
  }

  @Get('getPublicResourcAudio')
  getPublicResourcAudio() {
    return this.publicResourceService.getPublicResourcAudio();
  }

  @Get('getPublicResourcGeojson')
  getPublicResourcGeojson() {
    return this.publicResourceService.getPublicResourcGeojson();
  }

  @Get('getPublicResourcRaster')
  getPublicResourcRaster() {
    return this.publicResourceService.getPublicResourcRaster();
  }

  @Public()
  @Get('getFileAvatar/:id')
  async getFileAvatar(
    @Param('id') id: string,
    @Res() res: Response,
  ): Promise<void> {
    const publicItem =
      await this.publicResourceService.getPublicResourceById(id);
    let filePath = '';
    let folder = 'other'; // 默认文件夹名
    const contentType = 'image/png'; // 默认 Content-Type
    if (publicItem) {
      if (publicItem.raw_type.startsWith('image/')) {
        if (publicItem.raw_type === 'image/tiff') {
          filePath = path.resolve(process.env.FILE_PATH + '/image/tif.png');
        } else {
          folder = 'image';
        }
      } else if (publicItem.raw_type.startsWith('audio/')) {
        filePath = path.resolve(process.env.FILE_PATH + '/image/mp3.png');
      } else if (publicItem.raw_type === 'text/plain') {
        filePath = path.resolve(process.env.FILE_PATH + '/image/txt.png');
      } else if (publicItem.raw_type.startsWith('video/')) {
        filePath = path.resolve(process.env.FILE_PATH + '/image/mp4.png');
      } else if (publicItem.raw_type === 'application/json') {
        filePath = path.resolve(process.env.FILE_PATH + '/image/json.png');
      } else {
        filePath = path.resolve(process.env.FILE_PATH + '/image/file64.png');
      }
      if (!filePath) {
        filePath = path.resolve(
          `${process.env.FILE_PATH}/public/${folder}/${publicItem.src}`,
        );
      }
      res.setHeader('Content-Type', contentType);
      const fileStream = fs.createReadStream(filePath);
      fileStream.pipe(res);
    }
  }
}
